




//
//  NGovFBChildViewController.m
//  nncloud
//
//  Created by macfai on 2018/9/20.
//  Copyright © 2018年 youedata. All rights reserved.
#import "NGovFBChildViewController.h"
#import "NFeedbackCell.h"
#import "NFeedbackModel.h"
#import "NFilterView.h"
#import "NProblemContentViewController.h"
@interface NGovFBChildViewController ()<UITableViewDelegate,UITableViewDataSource,ITDatePickerControllerDelegate,STPickerSingleDelegate,NFilterViewDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger currentIndex;
@property (nonatomic,assign)NSInteger currentPage;
@property(nonatomic,strong)MJRefreshAutoNormalFooter *MJFooter;

//@property (nonatomic,copy)NSString *filterTime;

@property (nonatomic,strong)NFilterView *filterView;

///筛选时间
@property (nonatomic,copy)NSString *filterTime;
///筛选类型
@property (nonatomic,copy)NSString *filterType;
///筛选状态
@property (nonatomic,copy)NSString *filterStatus;


@end

#define cellIdentifier @"feedbackid"
//#define pageSize @"20"

@implementation NGovFBChildViewController

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64-49) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 80.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NFeedbackCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [ws refreshData];
        }];
    
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [ws loadMoreData];
        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    
    WeakStament(ws);
    //筛选
    NFilterView *filter = [[NFilterView alloc]initWithFrame:CGRectMake(0, ScreenHeight-64-50-40, ScreenWidth, 50)];
    filter.timeActionBolck = ^(UIButton * _Nonnull btn) {
        //添加年月选择器
        [ws yearMonthPicker];
    };
    filter.delegate = self;
    self.filterView = filter;
    [self.view addSubview:filter];
    
    self.currentIndex = -1;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshData) name:@"jjjj" object:nil];
}

- (void)yearMonthPicker {
    
    ITDatePickerController *datePickerController = [[ITDatePickerController alloc] init];
    datePickerController.tag = 630;
    datePickerController.delegate = self;
    datePickerController.showToday = NO;
    [self presentViewController:datePickerController animated:YES completion:nil];
    
}

#pragma mark - STPickerSingle delegate

- (void)filterPickerSingle:(STPickerSingle *)picker selectedTitle:(NSString *)selectedTitle selectIndex:(NSInteger)selectIndex{
    YYLog(@"%@",selectedTitle);
    if (picker.tag==111) {
        ///类型
        self.filterType = [NSString stringWithFormat:@"%li",selectIndex+1];
//        if (selectIndex>2) {
//            self.filterType = @"";
//        }
        YYLog(@"%@",self.filterType);
        [self.filterView.midBtn setTitle:[NSString stringWithFormat:@"类型：%@",selectedTitle] forState:UIControlStateNormal];
    }else{
        ///状态 这个分成了三部分，所以需要根据标题来判断然后传索引值
        // 0-10  @[@"待审核",@"等待受理",@"已受理",@"已转交相关部门",@"自行解答",@"等待用户确认",@"等待用户评价",@"已评价",@"已关闭",@"审核未通过",@"删除",]
        if ([selectedTitle isEqualToString:@"待审核"]) {
            self.filterStatus = [NSString stringWithFormat:@"%i",0];
        }else if ([selectedTitle isEqualToString:@"已受理"]){
            self.filterStatus = [NSString stringWithFormat:@"%i",1];
        }else if ([selectedTitle isEqualToString:@"已转交相关部门"]){
            self.filterStatus = [NSString stringWithFormat:@"%i",2];
        }else if ([selectedTitle isEqualToString:@"自行解答"]){
            self.filterStatus = [NSString stringWithFormat:@"%i",3];
        }else if ([selectedTitle isEqualToString:@"等待用户确认"]){
            self.filterStatus = [NSString stringWithFormat:@"%i",4];
        }else if ([selectedTitle isEqualToString:@"等待用户评价"]){
            self.filterStatus = [NSString stringWithFormat:@"%i",5];
        }else if ([selectedTitle isEqualToString:@"已评价"]){
            self.filterStatus = [NSString stringWithFormat:@"%i",6];
        }else if ([selectedTitle isEqualToString:@"已关闭"]){
            self.filterStatus = [NSString stringWithFormat:@"%i",7];
        }else if ([selectedTitle isEqualToString:@"审核未通过"]){
            self.filterStatus = [NSString stringWithFormat:@"%i",8];
        }else if ([selectedTitle isEqualToString:@"删除"]){
            self.filterStatus = [NSString stringWithFormat:@"%i",9];
        }else{
//             self.filterStatus = @"";
        }
        YYLog(@"%@",self.filterStatus);
        [self.filterView.rightBtn setTitle:[NSString stringWithFormat:@"状态：%@",selectedTitle] forState:UIControlStateNormal];
    }
    
    [self refreshData];
}


#pragma mark - 年月选择器
- (void)datePickerController:(ITDatePickerController *)datePickerController didSelectedDate:(NSDate *)date dateString:(NSString *)dateString{
    
    ///tag值真正应用起来，比如那种需要输入很多资料的资料框，每个市一行cell ，就可以用indexpath.row作为tag值就会比较方便
    
    if (datePickerController.tag==630) {
        
        YYLog(@"%@",dateString);
        
        NSMutableString *mut = [[NSMutableString alloc]initWithString:dateString];
        ///拼接成后台需要的数据格式
        [mut insertString:@"-" atIndex:4];
        
        self.filterTime = (NSString *)mut;
        
         [self.filterView.leftBtn setTitle:[NSString stringWithFormat:@"时间：%@",mut] forState:UIControlStateNormal];
        
        [self refreshData];
        
    }
}


- (void)refreshData {
    
    self.currentPage = 1;
    [self loadDataWithIndex:self.currentIndex];
}

- (void)loadMoreData {
    
    self.currentPage++;
    [self loadDataWithIndex:self.currentIndex];
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    YYLog(@"jjj");
    
}
-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
     YYLog(@"jjj");
}

- (void)formData:(NSArray *)data
{
    
    if (self.currentPage==1) {
         [self.dataArray removeAllObjects];
    }
    
    for (NSDictionary *dict in data) {
        
        NFeedbackModel *model = [NFeedbackModel mj_objectWithKeyValues:dict];
        [self.dataArray addObject:model];
    }
    [self.tableView  reloadData];
    [self endRefresh];
}


- (void)loadDataWithIndex:(NSInteger)index{
    //index 是滚动条的索引
    WeakStament(ws);
    NSNumber *satus; //问题状态 status (0.待处理,1.处理中,2.已处理)
    switch (index) {
        case 0:
        {
            satus = @(index+1);
        }
            break;
        case 1:
        {
            satus = @(index-1);
            
        }
            break;
        case 2:
        {
            satus = @(index);
        }
            break;
            
        default:
            break;
    }
    //questionUserId
    if (!satus) {
        satus = @(0);
    }
    [SVProgressHUD show];
    NSDictionary *dic = @{@"questionCreateTimeBegin":self.filterTime?:@"",@"questionType":self.filterType?:@"",@"questionProgress":self.filterStatus?self.filterStatus:@"",@"questionUserId":[NUserModel shareInstance].userInfoId,@"status":satus,@"size":@(kPageSize),@"curPage":@(self.currentPage)};
    [[NNRCNetRequest manager]getWithUrl:kGovQuestionListUrl params:dic success:^(id result) {
        YYLog(@"%@",result);
        NSInteger code = [NNRCNetRequest getStatusCodeWithObj:result];
        if (code==0) {
            NSArray *arr = result[@"page"][@"records"];
            [ws formData:arr];
            if (arr.count==0) {
                [ws noMoreData];
            }
        }else{
            [AFToast showText:result[@"msg"]];
        }
        [SVProgressHUD dismiss];
    } failure:^(id failure) {
        YYLog(@"%@",failure);
        [self endRefresh];
        [SVProgressHUD dismiss];
    }];
}

- (void)noMoreData {
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
}
- (void)endRefresh {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NFeedbackModel *model = self.dataArray[indexPath.row];
    NProblemContentViewController *proc = [NProblemContentViewController new];
    proc.hidesBottomBarWhenPushed = YES;
    proc.problemID = [NSString stringWithFormat:@"%ld",(long)model.questionId];
    [self.navigationController pushViewController:proc animated:YES];
}

#pragma mark - datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NFeedbackModel *model = self.dataArray[indexPath.row];
    NFeedbackCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.flag = @"fee";
    [cell configWithData:model];
    return cell;
}

#pragma mark ZJScrollPageViewChildVcDelegate
- (void)zj_viewWillAppearForIndex:(NSInteger)index{
    YYLog(@"%ld",index);
    [NUserModel shareInstance].index = index;
    self.currentIndex = index;
}
- (void)zj_viewDidAppearForIndex:(NSInteger)index{
    
}
- (void)zj_viewWillDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidLoadForIndex:(NSInteger)index{
    //这里具体处理请求逻辑不同的页面根据索引进行请求
    YYLog(@"%ld",index);
//    self.currentIndex = index;
//    [self loadDataWithIndex:index];
     [self loadDataWithIndex:[NUserModel shareInstance].index];
}
@end
